package com.halfsword.creativecraft.item.custom;

import com.halfsword.creativecraft.sound.Sounds;
import com.halfsword.creativecraft.utils.SpaceTags;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class SpaceDetectorItem extends Item {

    public SpaceDetectorItem(Properties p_41383_) {
        super(p_41383_);
    }

    @Override
    public InteractionResult useOn(UseOnContext p_41427_) {
        if (!p_41427_.getLevel().isClientSide()) {
            BlockPos positionClicked = p_41427_.getClickedPos();
            Player player = p_41427_.getPlayer();
            boolean foundBlock = false;
            for(int i = 0 ;i <= positionClicked.getY() + 64;i++) {
                BlockState state = p_41427_.getLevel().getBlockState(positionClicked.below(i));
                if(isValuableBlock(state)) {
                    outputValuableCoordinates(positionClicked.below(i),player,state.getBlock());
                    foundBlock = true;
                    p_41427_.getLevel().playSeededSound(null,positionClicked.getX(),positionClicked.getY(),positionClicked.getZ(), Sounds.SPACE_DETECTOR_FOUND_ORE.get(), SoundSource.BLOCKS,1f,1f,0);
                    break;
                }
            }
            if(!foundBlock) {
                player.sendSystemMessage(Component.translatable("tooltip.creativecraft.space_detector.tooltip0"));
            }

        }
        p_41427_.getItemInHand().hurtAndBreak(1, p_41427_.getPlayer(), player -> player.broadcastBreakEvent(player.getUsedItemHand()));
        return InteractionResult.SUCCESS;
    }

    @Override
    public void appendHoverText(ItemStack p_41421_, @Nullable Level p_41422_, List<Component> p_41423_, TooltipFlag p_41424_) {
        p_41423_.add(Component.translatable("tooltip.creativecraft.space_detector.tooltip1"));
        super.appendHoverText(p_41421_, p_41422_, p_41423_, p_41424_);
    }

    private void outputValuableCoordinates(BlockPos blockPos, Player player, Block block) {
        player.sendSystemMessage(Component.translatable("tooltip.creativecraft.space_detector.tooltip2").append(I18n.get(block.getDescriptionId()) ).append(Component.translatable("tooltip.creativecraft.space_detector.tooltip3")).append( " [ "+blockPos.getX()+" , "+blockPos.getY()+" , "+blockPos.getZ()+" ]"));

    }

    private boolean isValuableBlock(BlockState state) {
        return state.is(SpaceTags.Blocks.SPACEDECECTOR_VALUABLES);
    }
}
